﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Oxite.Validation;
using ECube.Web.CMS.Models;
using ECube.Web.CMS.Repositories;
using ECube.Web.CMS.Extensions;
namespace ECube.Web.CMS
{
    public class SiteNavigatorService:ISiteNavigatorService
    {
        static Dictionary<String, SiteNavigatorService> items = new Dictionary<string,SiteNavigatorService>();
        static SiteNavigatorService _Default;

        private SiteMapProvider provider;
        private String _Name = "";

        private SiteNavigatorService(String navFamliyName)
        {
            if (String.IsNullOrEmpty(navFamliyName))
            {
                provider = SiteMap.Provider;
                _Name = provider.Name;
            }
            else
            {
                _Name = navFamliyName;
                provider = SiteMap.Providers[navFamliyName];
            }

            items.Add(_Name, this);
        }

        public static Dictionary<String, SiteNavigatorService> Items
        {
            get
            {
                if (items == null) 
                {
                    items = new Dictionary<String, SiteNavigatorService>();                    
                }
                return items;
            }
        }

        protected static SiteNavigatorService Default
        {
            get
            {
                if (_Default == null)
                {
                    _Default = new SiteNavigatorService(null);                    
                }
                return _Default;
            }

        }
        public static SiteNavigatorService Current
        {
            set
            {
                HttpContext.Current.Session["ECube.Web.SiteNavigator.Current"] = value;
            }
            get
            {
                SiteNavigatorService current = HttpContext.Current.Session["ECube.Web.SiteNavigator.Current"] as SiteNavigatorService;
                if (current == null)
                {
                    current = SiteNavigatorService.Default;
                }
                return current;
            }
        }



        public static SiteNavigatorService Create(String navFamilyName)
        {
            SiteNavigatorService service = Items.ContainsKey(navFamilyName) ?  Items[navFamilyName] : null;
            if (service == null)
            {
                service = new SiteNavigatorService(navFamilyName);
            }
            return service;
        }

        public String Name
        {
            get
            {
                return _Name;
            }
        }
        public SiteMapNode NewNode(MvcSiteMapNodeInput input)
        {
            if (string.IsNullOrEmpty(input.Key))
            {
                input.Key = Guid.NewGuid().ToString();
            }
            MvcSiteMapNode node = new MvcSiteMapNode(provider, input.Key,null);
            node.FromInput(input);
            return node;
        }
        
        public bool AddNode(SiteMapNode node,string parentKey)
        {
            if (((IMvcSiteMapProvider)provider).AddNode(node,parentKey))
            {
                return Save();
            }
            return false;
        }
        public SiteMapNode FindNode(String key)
        {
            return provider.FindSiteMapNodeFromKey(key);
        }
        public SiteMapNode GetNode(String rawUrl)
        {
            return provider.FindSiteMapNode(rawUrl);
        }
        public Boolean RemoveNode(String key)
        {
            if (provider is IMvcSiteMapProvider)
            {
                return ((IMvcSiteMapProvider)provider).RemoveNode(key);
            }
            return false;
        }
        public Boolean Move(String sourceKey, String targetKey, NodeMovePosition position)
        {
            if (provider is IMvcSiteMapProvider)
            {
                return ((IMvcSiteMapProvider)provider).Move(sourceKey, targetKey, position);
            }
            return false;
         
        }
        public Boolean Save()
        {
            if (provider is IMvcSiteMapProvider)
            {
                ((IMvcSiteMapProvider)provider).Save();
            }
            return true;
        }

        public ValidationError ValidateKey(string key)
        {
            if (provider.FindSiteMapNodeFromKey(key) != null)
            {
                return new ValidationError("key", key,string.Format("已存在KEY值为“{0}”的节点！",key));
            }
            return null;
        }
        public ValidationError ValidateUrl(string url)
        {
            return new ValidationError("url", url, "已存在值！");
        }


        #region ISiteNavigatorService 成员

        public MvcSiteMapNode RootNode
        {
            get 
            {
                return provider.RootNode as MvcSiteMapNode;                
            }
        }
        #endregion
    }
}
